package com.yanqu.road.dao.impl.activity.jiuzhou;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.jiuzhou.round.CrossJiuZhouRoundData;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class JiuZhouRoundDataDaoImpl extends TempDataObjectDao<CrossJiuZhouRoundData> {
    public static String getCrossJiuZhouRoundDataSql = "select * from `t_c_jiu_zhou_round_data` where `activityId`=? and `zoneType`=?";
    public CrossJiuZhouRoundData getCrossJiuZhouRoundData(int activityId,int zoneType) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossJiuZhouRoundDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, zoneType);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String getCrossJiuZhouRoundDataMapSql = "select * from `t_c_jiu_zhou_round_data` where `activityId`=?";
    public Map<Integer,CrossJiuZhouRoundData> getCrossJiuZhouRoundDataMap(int activityId) {
        Map<Integer,CrossJiuZhouRoundData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossJiuZhouRoundDataMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossJiuZhouRoundData parse = parse(rs);
                map.put(parse.getZoneType(),parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossJiuZhouRoundDataSql = "update `t_c_jiu_zhou_round_data` set curRoundId=?,curRoundState=?,roundType=?,isLockAbility=?,lockAbility=?,roundPromotionNumMap=?,serverDataState=?,championId=? where activityId=? and zoneType=?";
    @Override
    protected boolean updateData(CrossJiuZhouRoundData crossJiuZhouRoundData) {
        crossJiuZhouRoundData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoundData.getCurRoundId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoundData.getCurRoundState()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoundData.getRoundType()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoundData.getIsLockAbility()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossJiuZhouRoundData.getLockAbility().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouRoundData.getRoundPromotionNumMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoundData.getServerDataState()));
        para.put(i++, new DbParameter(Types.BIGINT, crossJiuZhouRoundData.getChampionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoundData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoundData.getZoneType()));
        if (getDb().executeNoneQuery(updateCrossJiuZhouRoundDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossJiuZhouRoundDataSql = "insert into `t_c_jiu_zhou_round_data`(activityId,zoneType,curRoundId,curRoundState,roundType,isLockAbility,lockAbility,roundPromotionNumMap,serverDataState,championId) values (?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossJiuZhouRoundData crossJiuZhouRoundData) {
        crossJiuZhouRoundData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoundData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoundData.getZoneType()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoundData.getCurRoundId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoundData.getCurRoundState()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoundData.getRoundType()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoundData.getIsLockAbility()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossJiuZhouRoundData.getLockAbility().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouRoundData.getRoundPromotionNumMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoundData.getServerDataState()));
        para.put(i++, new DbParameter(Types.BIGINT, crossJiuZhouRoundData.getChampionId()));
        if (getDb().executeNoneQuery(insertCrossJiuZhouRoundDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossJiuZhouRoundData parseData(ResultSet rs) throws SQLException {
        CrossJiuZhouRoundData crossJiuZhouRoundData = new CrossJiuZhouRoundData();
        crossJiuZhouRoundData.setActivityId(rs.getInt("activityId"));
        crossJiuZhouRoundData.setZoneType(rs.getInt("zoneType"));
        crossJiuZhouRoundData.setCurRoundId(rs.getInt("curRoundId"));
        crossJiuZhouRoundData.setCurRoundState(rs.getInt("curRoundState"));
        crossJiuZhouRoundData.setRoundType(rs.getInt("roundType"));
        crossJiuZhouRoundData.setIsLockAbility(rs.getInt("isLockAbility"));
        crossJiuZhouRoundData.setLockAbility(new BigInteger(rs.getString("lockAbility")));
        crossJiuZhouRoundData.setRoundPromotionNumMap(JSON.parseObject(rs.getString("roundPromotionNumMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.lang.Integer>>() {}));
        crossJiuZhouRoundData.setServerDataState(rs.getInt("serverDataState"));
        crossJiuZhouRoundData.setChampionId(rs.getLong("championId"));

        return crossJiuZhouRoundData;
    }


}
